//
//  MCUserDefaults.m
//  MClient
//
//  Created by  史正烨 on 10-8-25.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "MCUserDefaults.h"
#import "MClientAppDelegate.h"
#import "MCFunction.h"
//#import "ClassFunctionAddition.h"

static MCUserDefaults *userDefaultsInstance = nil;


@implementation MCUserDefaults
@synthesize isDemoApplication;
@synthesize guestDefaultLogin;
@synthesize logoHeader = _needLogoHeader;
@synthesize packegeSkinNumber;
@synthesize needScreenVHChange;
@synthesize needModifyPassword;
@synthesize needServerEditor;
@synthesize useShrinkTreeView;
@synthesize dragListLastAndMore;
@synthesize remoteNotification;
@synthesize useLastMenuAsShortKey;
@synthesize contactSync;

+ (MCUserDefaults *)instance {
	@synchronized(self) {
		if (userDefaultsInstance == nil) {
			userDefaultsInstance = [[self alloc] init];
		}
	}
	return userDefaultsInstance;
}


- (id)init
{
	if (self = [super init]) {
        // Custom initialization
		defaults = [NSUserDefaults standardUserDefaults];
		skinsPlist = nil;
		
		NSString *path = [[NSBundle mainBundle] pathForResource:@"MClientConstant" ofType:@"plist"];
		NSDictionary* dictionary = [[NSDictionary alloc]initWithContentsOfFile:path];
		self.currentReq = 0;
		self.currentRes = 0;
		defaultServerUrl = [[dictionary objectForKey:@"serverUrl"] retain];
		isDemoApplication = [[dictionary objectForKey:@"isDemoApplication"] boolValue];
		_needLogoHeader = [[dictionary objectForKey:@"needLogoHeader"] retain];
		needScreenVHChange = [[dictionary objectForKey:@"needScreenVHChange"] boolValue];
		needModifyPassword = [[dictionary objectForKey:@"needModifyPassword"] boolValue];
		needServerEditor = [[dictionary objectForKey:@"needServerEditor"] boolValue];
		packegeSkinNumber = [[dictionary objectForKey:@"packegeSkinNumber"] intValue];
		useShrinkTreeView = [[dictionary objectForKey:@"useShrinkTreeView"] boolValue];
		dragListLastAndMore = [[dictionary objectForKey:@"dragListLastAndMore"] boolValue];
		remoteNotification = [[dictionary objectForKey:@"remoteNotification"] boolValue];
		useLastMenuAsShortKey = [[dictionary objectForKey:@"useLastMenuAsShortKey"] boolValue];
		contactSync = [[dictionary objectForKey:@"contactSync"] boolValue];
        [self setIsGridMenu:[[dictionary objectForKey:@"isGridMenu"] boolValue]];//huotianjun
        [self setIsSavePassword:[[dictionary objectForKey:@"isSavePassword"] boolValue]];//huotianjun
        [self setIsAutoLogin:[[dictionary objectForKey:@"isAutoLogin"] boolValue]];//huotianjun
        [self setIsSetupFirst:[[dictionary objectForKey:@"isSetupFirst"] boolValue]];//huotianjun

        		
		if ([defaults objectForKey:@"current_GuestUserId"]) {
			[defaults removeObjectForKey:@"current_GuestUserId"];
		}
		/*
		if ([defaults objectForKey:@"ctime"]) {
			[defaults removeObjectForKey:@"ctime"];
		}*/
		if ([dictionary objectForKey:@"GuestDefaultLogin"]) {
			guestDefaultLogin = [[NSDictionary dictionaryWithDictionary:[dictionary objectForKey:@"GuestDefaultLogin"]] retain];
			if (![[guestDefaultLogin objectForKey:@"isValid"] boolValue]) {
				[guestDefaultLogin release];
				guestDefaultLogin = nil;
			}
		} else {
			guestDefaultLogin = nil;
		}
		if ([dictionary objectForKey:@"LoginWithHidePartyId"]) {
			LoginWithHidePartyId = [[NSMutableDictionary dictionaryWithDictionary:[dictionary objectForKey:@"LoginWithHidePartyId"]] retain];
			if (![[LoginWithHidePartyId objectForKey:@"isValid"] boolValue]) {
				[LoginWithHidePartyId release];
				LoginWithHidePartyId = nil;
			}
		} else {
			LoginWithHidePartyId = nil;
		}
		
		[dictionary release];
    }
    return self;
}

- (void)dealloc
{
	[LoginWithHidePartyId release];
	[_needLogoHeader release];
	[defaultServerUrl release];
	[guestDefaultLogin release];
	[skinsPlist release];
	[super dealloc];
}

- (BOOL)needHidePartyId
{
	return LoginWithHidePartyId && [[LoginWithHidePartyId objectForKey:@"isValid"] boolValue];
}

// 存取“是否隐藏会员名”标志
- (bool) isHidePartyId
{
	if (self.needHidePartyId) {
		return YES;
	}
	return [[defaults objectForKey:@"isHidePartyId"] boolValue];
}
- (void) setIsHidePartyId:(bool)isHidePartyId
{
	[defaults setBool:isHidePartyId forKey:@"isHidePartyId"];
}

// 存取“是否保存密码”标志
- (bool) isSavePassword
{
	if (guestDefaultLogin) {
		return YES;
	}else {
		return [[defaults objectForKey:@"isSavePassword"] boolValue];
	}
}
- (void) setIsSavePassword:(bool)isSavePassword
{
	[defaults setBool:isSavePassword forKey:@"isSavePassword"];
}

// 存取“是否自动登录”标志
- (bool) isAutoLogin
{
	if (guestDefaultLogin) {
		return YES;
	} else {
		return [[defaults objectForKey:@"isAutoLogin"] boolValue];
	}	
}

- (void) setIsAutoLogin:(bool) isAutoLogin
{
	[defaults setBool:isAutoLogin forKey:@"isAutoLogin"];
}

// 存取“是否显示Help”标志
- (bool) isShowHelp
{
	return [[defaults objectForKey:@"isHelpHidden"] boolValue];
}
- (void) setIsShowHelp:(bool) isShowHelp
{
	[defaults setBool:isShowHelp forKey:@"isHelpHidden"];
}

// 存取
- (bool) isGridMenu
{
	return [[defaults objectForKey:@"isGridMenu"] boolValue];
}
- (void) setIsGridMenu:(bool) isGridMenu
{
	[defaults setBool:isGridMenu forKey:@"isGridMenu"];
}

// 附件(doc、docx、txt)预览调用平台接口
- (bool) isShowOpenOffice
{
	return [[defaults objectForKey:@"isShowOpenOffice"] boolValue];
}
- (void) setIsShowOpenOffice:(bool) isShowOpenOffice
{
	[defaults setBool:isShowOpenOffice forKey:@"isShowOpenOffice"];
}

// 存取
- (int) currentReq
{
	return [[defaults objectForKey:@"currentReq"] intValue];
}
- (void) setCurrentReq:(int) currentReq
{
	[defaults setObject:[NSNumber numberWithInteger:currentReq] forKey:@"currentReq"];
}

// 存取
- (int) currentRes
{
	return [[defaults objectForKey:@"currentRes"] intValue];
}
- (void) setCurrentRes:(int) currentRes
{
	[defaults setObject:[NSNumber numberWithInteger:currentRes] forKey:@"currentRes"];
}

// 存取
- (int) totalReq
{
	return [[defaults objectForKey:@"totalReq"] intValue];
}
- (void) setTotalReq:(int) totalReq
{
	[defaults setObject:[NSNumber numberWithInteger:totalReq] forKey:@"totalReq"];
}

// 存取
- (int) totalRes
{
	return [[defaults objectForKey:@"totalRes"] intValue];
}
- (void) setTotalRes:(int) totalRes
{
	[defaults setObject:[NSNumber numberWithInteger:totalRes] forKey:@"totalRes"];
}

// 存取服务器地址列表，其下面是"账号列表"
- (NSMutableDictionary*) serverList
{
	return [defaults objectForKey:@"serverList"];
}

- (void) setServerList:(NSMutableDictionary*) srvList
{
	[defaults setObject:srvList forKey:@"serverList"];
}

// 存取当前服务器地址
- (NSString*) current_ServerUrl
{
	if (guestDefaultLogin) {
		return defaultServerUrl;
	} else {
		NSString *retValue = [defaults objectForKey:@"current_ServerUrl"];
		if (nil == retValue) {
			return [MClientAppDelegate getDefaultServerUrl];
		} else {
			return retValue;
		}
	}
}

- (void) setCurrent_ServerUrl:(NSString*) current_ServerUrl
{
	[defaults setObject:current_ServerUrl forKey:@"current_ServerUrl"];
}

// 存取当前会员
- (NSString*) current_PartyId
{
	if (guestDefaultLogin) {
		return [guestDefaultLogin objectForKey:@"PartyId"];
	} else {
		if (self.needHidePartyId) {
			return [LoginWithHidePartyId objectForKey:@"PartyId"];
		}
		
		NSString *retValue = [defaults objectForKey:@"current_PartyId"];
		return nil==retValue ? @"":retValue;
	}
}

- (void) setCurrent_PartyId:(NSString*) current_PartyId
{
	[defaults setObject:current_PartyId forKey:@"current_PartyId"];
	if (self.needHidePartyId) {
		[LoginWithHidePartyId setObject:current_PartyId forKey:@"PartyId"];
	}
}

// 存取当前用户
- (NSString*) current_UserId
{
	if (guestDefaultLogin) {
		return [guestDefaultLogin objectForKey:@"UserId"];
	} else {
		NSString *retValue = [defaults objectForKey:@"current_UserId"];
		return nil==retValue ? @"":retValue;
	}
}

- (void) setCurrent_UserId:(NSString*) current_UserId
{
	[defaults setObject:current_UserId forKey:@"current_UserId"];
}

// 存取当前用户密码
- (NSString*) current_Password
{
	if (guestDefaultLogin) {
		return [guestDefaultLogin objectForKey:@"PWord"];
	} else {
		NSDictionary* userList = [self.serverList objectForKey:self.current_ServerUrl];
		NSString *currentUser = [NSString stringWithFormat:@"%@||%@",self.current_PartyId, self.current_UserId];
		NSDictionary* userInfo = [userList objectForKey:currentUser];
		NSString *retValue = [userInfo objectForKey:@"password"];
		return nil==retValue ? @"":retValue;
	}
}

- (void) setCurrent_Password:(NSString*) current_Password
{
	//if ([self.current_Password isEqualToString:current_Password]) {
	//	return;
	//}
	NSString *currentUser = [NSString stringWithFormat:@"%@||%@",self.current_PartyId, self.current_UserId];
	NSDictionary* pTemp = self.serverList;
	if (pTemp == nil) {
		NSMutableDictionary *temp1 = [NSMutableDictionary dictionary];
		NSMutableDictionary *temp2 = [NSMutableDictionary dictionary];
		NSMutableDictionary *temp3 = [NSMutableDictionary dictionary];
		[temp1 setObject:current_Password forKey:@"password"];
		[temp2 setObject:temp1 forKey:currentUser];
		[temp3 setObject:temp2 forKey:self.current_ServerUrl];
		self.serverList = temp3;
		return;
	}
	NSMutableDictionary* srvList = [NSMutableDictionary dictionary];
    [srvList addEntriesFromDictionary:pTemp];
	
	pTemp = [srvList objectForKey:self.current_ServerUrl];
	NSMutableDictionary* userList;
	if (pTemp == nil) {
		NSMutableDictionary *temp1 = [NSMutableDictionary dictionary];
		userList = [NSMutableDictionary dictionary];
		[temp1 setObject:current_Password forKey:@"password"];
		[userList setObject:temp1 forKey:currentUser];
		[srvList setObject:userList forKey:self.current_ServerUrl];
		self.serverList = srvList;
		return;
	}
	userList = [NSMutableDictionary dictionary];
    [userList addEntriesFromDictionary:pTemp];
	
	NSMutableDictionary* userInfo = [NSMutableDictionary dictionary];
    [userInfo addEntriesFromDictionary:[userList objectForKey:currentUser]];
	[userInfo setValue:current_Password forKey:@"password"];
	[userList setObject:userInfo forKey:currentUser];
	[srvList  setObject:userList forKey:self.current_ServerUrl];
	self.serverList = srvList;
}

// 手机百事通存取当前用户
- (NSString*) current_GuestUserId
{
	if ([defaults objectForKey:@"current_GuestUserId"]) {
		return [defaults objectForKey:@"current_GuestUserId"];
	}
	NSString *retValue = [guestDefaultLogin objectForKey:@"UserId"];
	return nil==retValue ? @"":retValue;
}

- (void) setCurrent_GuestUserId:(NSString*) current_GuestUserId
{
	[defaults setObject:current_GuestUserId forKey:@"current_GuestUserId"];
}

// 存取最后接收消息时间
- (NSString*) ctime
{
	NSString *retValue = [defaults objectForKey:@"ctime"];
	return nil==retValue ? @"":retValue;
}

- (void) setCtime:(NSString*) ctime
{
	[defaults setObject:ctime forKey:@"ctime"];
}

// 设置收取消息时间
- (int) msgTime
{
	if (![defaults objectForKey:@"msgTime"]) {
		return 300;
	}
	return [[defaults objectForKey:@"msgTime"] intValue];
}

- (void) setMsgTime:(int) msgTime
{
	[defaults setObject:[NSNumber numberWithInteger:msgTime] forKey:@"msgTime"];
}

#pragma mark ------换肤相关------

- (NSString*) bundlePath
{
	return [defaults objectForKey:@"bundlePath"];
}

- (void) setBundlePath:(NSString*) bundlePath
{
	[defaults setObject:bundlePath forKey:@"bundlePath"];
}

- (BOOL)isFirstUse
{
	NSString *retValue = [self bundlePath];
	NSBundle *mainBundle = [NSBundle mainBundle];
	
	if (retValue == nil) {
		self.bundlePath = [mainBundle bundlePath];
		return YES;
	} else if ([retValue isEqualToString:[mainBundle bundlePath]]) {
		return NO;
	} else {
		return YES;
	}
}

- (bool ) isSetupFirst
{
	return [[defaults objectForKey:@"isSetupFirst"] boolValue];
}

- (void) setIsSetupFirst:(bool)isSetupFirst
{
	[defaults setBool:isSetupFirst forKey:@"isSetupFirst"];
}

// 存取当前用户
- (int) skin_index
{
	return [[defaults objectForKey:@"skin_index"] intValue];
}
- (void) setSkin_index:(int) skin_index
{
	[defaults setObject:[NSNumber numberWithInteger:skin_index] forKey:@"skin_index"];
}

- (UIColor*) viewCellTitleBackgroundColor {
	return [UIColor clearColor];
}

- (UIImage*)getImage:(NSString*)imageName withIndex:(NSInteger)index additonText:(NSString*)additon
{
	NSFileManager* fileManager = [NSFileManager defaultManager];
	NSString* imageDownload, *imageDefault;
	
	imageDownload = [[MCFunction documentPath] stringByAppendingPathComponent:
					 [NSString stringWithFormat:@"skins/%@%@_no%d.png", additon, imageName,index+1]];
	if ([UIImage imageNamed:[NSString stringWithFormat:@"%@_no%d.png", imageName, index]]) {
		imageDefault = [NSString stringWithFormat:@"%@_no%d.png", imageName, index];
	} else {
		imageDefault = [NSString stringWithFormat:@"%@_%@.png", imageName, @"default"];
	}
	
	if (self.skinsPlist) {
		if ([fileManager fileExistsAtPath:imageDownload] && ![self isFirstUse]) {
			UIImage* image = [UIImage imageWithContentsOfFile:imageDownload];
			if (image) {
				return image;
			} else {
				return [UIImage imageNamed:imageDefault];
			}
			
		} else {
			return [UIImage imageNamed:imageDefault];
		}
	} else {
		return [UIImage imageNamed:imageDefault];
	}
}

- (UIImage*)bgViewImage:(int)index
{
	return [self getImage:@"content_bg" withIndex:index additonText:self.isDemoApplication?@"demo_":@"app_"];
}

- (UIImage*) skin_bgViewImg
{
	return [self getImage:@"content_bg" withIndex:self.skin_index additonText:self.isDemoApplication?@"demo_":@"app_"];
}

- (UIImage*) skin_bgMenuCellImg
{
	return [self getImage:@"menu_bg" withIndex:self.skin_index additonText:self.isDemoApplication?@"demo_":@"app_"];
}

- (UIImage*) skin_bgListLabelImg
{
	return [self getImage:@"list_label_bg" withIndex:self.skin_index additonText:self.isDemoApplication?@"demo_":@"app_"];
}

- (UIImage*) skin_bgSectionImg
{
	return [self getImage:@"section_bg" withIndex:self.skin_index additonText:self.isDemoApplication?@"demo_":@"app_"];
}

- (UIImage*) skin_bgLoginImg
{
	return [self getImage:@"login_bg" withIndex:self.skin_index additonText:self.isDemoApplication?@"demo_":@"app_"];
}

- (UIImage*) skin_btnLoginImg
{
	return [self getImage:[NSString stringWithFormat:@"%@btn_login",self.isDemoApplication?@"demo_":@"app_"]
				withIndex:self.skin_index
			  additonText:@""];
}

- (UIImage*) skin_btnAboutImg
{
	return [self getImage:[NSString stringWithFormat:@"%@btn_about",self.isDemoApplication?@"demo_":@"app_"] 
				withIndex:self.skin_index
			  additonText:@""];
}

- (UIImage*) skin_btnAccountImg
{
	return [self getImage:[NSString stringWithFormat:@"%@btn_account",self.isDemoApplication?@"demo_":@"app_"] 
				withIndex:self.skin_index
			  additonText:@""];
}

- (UIImage*) skin_btnConfigImg
{
	return [self getImage:[NSString stringWithFormat:@"%@btn_config",self.isDemoApplication?@"demo_":@"app_"] 
				withIndex:self.skin_index
			  additonText:@""];
}

- (UIImage*) skin_btnDemoImg
{
	return [self getImage:[NSString stringWithFormat:@"%@btn_demo",self.isDemoApplication?@"demo_":@"app_"] 
				withIndex:self.skin_index
			  additonText:@""];
}

- (UIImage*)skin_logintitleImg
{
	return [self getImage:@"title" withIndex:self.skin_index additonText:self.isDemoApplication?@"demo_":@"app_"];
}

- (UIImage*)skin_loginlogoImg
{
	return [self getImage:@"logo" withIndex:self.skin_index additonText:self.isDemoApplication?@"demo_":@"app_"];
}

- (UIImage*)skin_logoTitle
{
	return [self getImage:[_needLogoHeader objectForKey:@"logoName"] withIndex:self.skin_index additonText:self.isDemoApplication?@"demo_":@"app_"];
}

- (UIImage*)skin_withFormButtonBg
{
	return [self getImage:@"withform_button" withIndex:self.skin_index additonText:self.isDemoApplication?@"demo_":@"app_"];
}

- (UIImage*) skin_bgAListEvenBg
{
	return [self getImage:@"menu_bg" withIndex:self.skin_index additonText:self.isDemoApplication?@"demo_":@"app_"];
}

- (UIImage*) skin_bgAListOddBg
{
	return [self getImage:@"menu_bg" withIndex:self.skin_index additonText:self.isDemoApplication?@"demo_":@"app_"];
}

#pragma mark ------换肤颜色相关------
- (UIColor*) skin_navigationBarColor
{
	return [self navigationBarColor:self.skin_index];
}

- (UIColor*)navigationBarColor:(int)index
{
	if (self.skinsPlist) {
		NSArray* skins = [self.skinsPlist objectForKey:@"skin_lists"];
		NSDictionary* skin_item = [skins objectAtIndex:index];

		NSString *barColor = [skin_item objectForKey:@"navigationBarTintColor"];
		if (barColor&&![barColor isEmptyOrWhitespace]) {
			return [MCFunction hexColor:barColor];
		} else {
			goto Default_Defined;
		}
	} else {
	Default_Defined:
		switch (index) {
			case 1:
				return [MCFunction hexColor:@"2FB47813"];
			case 2:
				return [MCFunction hexColor:@"488A8713"];
			default:
				return [MCFunction hexColor:@"0c7ad913"];
		} 
	}
}

- (UIColor*) skin_tabBarColor
{
	if (self.skinsPlist) {
		NSArray* skins = [self.skinsPlist objectForKey:@"skin_lists"];
		NSDictionary* skin_item = [skins objectAtIndex:self.skin_index];
		
		NSString *barColor = [skin_item objectForKey:@"tabBarColor"];
		if (barColor&&![barColor isEmptyOrWhitespace]) {
			return [MCFunction hexColor:barColor];
		} else {
			goto Default_Defined;
		}
	} else {
	Default_Defined:
		switch (self.skin_index) {
			case 1:
				return [MCFunction hexColor:@"00664564"];
			case 2:
				return [MCFunction hexColor:@"00664564"];
			default:
				return [MCFunction hexColor:@"09519364"];
		} 
	}
}

- (UIColor*) skin_bgListLabelSpaceColor
{
	if (self.skinsPlist) {
		NSArray* skins = [self.skinsPlist objectForKey:@"skin_lists"];
		NSDictionary* skin_item = [skins objectAtIndex:self.skin_index];
		
		NSString *barColor = [skin_item objectForKey:@"bgListLabelSpaceColor"];
		if (barColor&&![barColor isEmptyOrWhitespace]) {
			return [MCFunction hexColor:barColor];
		} else {
			goto Default_Defined;
		}
	} else {
	Default_Defined:
		switch (self.skin_index) {
			case 1:
				return [MCFunction hexColor:@"288C6464"];
			case 2:
				return [MCFunction hexColor:@"51898664"];
			default:
				return [MCFunction hexColor:@"0055AA64"];
		} 
	}
}

- (UIColor*) skin_bgTableEvenColor
{
	if (self.skinsPlist) {
		NSArray* skins = [self.skinsPlist objectForKey:@"skin_lists"];
		NSDictionary* skin_item = [skins objectAtIndex:self.skin_index];
		
		NSString *barColor = [skin_item objectForKey:@"bgTableEvenColor"];
		if (barColor&&![barColor isEmptyOrWhitespace]) {
			return [MCFunction hexColor:barColor];
		} else {
			goto Default_Defined;
		}
	} else {
	Default_Defined:
		switch (self.skin_index) {
			case 1:
				return [MCFunction hexColor:@"F7FFF764"];
			case 2:
				return [MCFunction hexColor:@"6AAEAB64"];
			default:
				return [MCFunction hexColor:@"EEF8FD64"];
		} 
	}
}

- (UIColor*) skin_bgTableOddColor
{
	if (self.skinsPlist) {
		NSArray* skins = [self.skinsPlist objectForKey:@"skin_lists"];
		NSDictionary* skin_item = [skins objectAtIndex:self.skin_index];
		
		NSString *barColor = [skin_item objectForKey:@"bgTableOddColor"];
		if (barColor&&![barColor isEmptyOrWhitespace]) {
			return [MCFunction hexColor:barColor];
		} else {
			goto Default_Defined;
		}
	} else {
	Default_Defined:
		switch (self.skin_index) {
			case 1:
				return [UIColor whiteColor];
			case 2:
				return [MCFunction hexColor:@"7BB7B464"];	
			default:
				return [UIColor whiteColor];
		} 
	}
}

- (UIColor*) skin_segmentedControlColor
{
	if (self.skinsPlist) {
		NSArray* skins = [self.skinsPlist objectForKey:@"skin_lists"];
		NSDictionary* skin_item = [skins objectAtIndex:self.skin_index];
		
		NSString *barColor = [skin_item objectForKey:@"segmentedControlColor"];
		if (barColor&&![barColor isEmptyOrWhitespace]) {
			return [MCFunction hexColor:barColor];
		} else {
			goto Default_Defined;
		}
	} else {
	Default_Defined:
		switch (self.skin_index) {
			case 1:
				return [MCFunction hexColor:@"53D29913"];
			case 2:
				return [MCFunction hexColor:@"7BB7B464"];	
			default:
				return [MCFunction hexColor:@"379AF413"];
		} 
	}
}

- (UIColor*) skin_withFormButtonFontColor
{
	if (self.skinsPlist) {
		NSArray* skins = [self.skinsPlist objectForKey:@"skin_lists"];
		NSDictionary* skin_item = [skins objectAtIndex:self.skin_index];
		
		NSString *barColor = [skin_item objectForKey:@"withFormButtonFontColor"];
		if (barColor&&![barColor isEmptyOrWhitespace]) {
			return [MCFunction hexColor:barColor];
		} else {
			goto Default_Defined;
		}
	} else {
	Default_Defined:
		switch (self.skin_index) {
			case 1:
				return [MCFunction hexColor:@"00996664"];
			case 2:
				return [UIColor whiteColor];	
			default:
				return [MCFunction hexColor:@"6699CC64"];
		} 
	}
}

- (UIColor*) skin_menuFontColor
{
	if (self.skinsPlist) {
		NSArray* skins = [self.skinsPlist objectForKey:@"skin_lists"];
		NSDictionary* skin_item = [skins objectAtIndex:self.skin_index];
		
		NSString *barColor = [skin_item objectForKey:@"menuFontColor"];
		if (barColor&&![barColor isEmptyOrWhitespace]) {
			return [MCFunction hexColor:barColor];
		} else {
			goto Default_Defined;
		}
	} else {
	Default_Defined:
		switch (self.skin_index) {
			case 1:
				return [UIColor blackColor];
			case 2:
				return [UIColor whiteColor];	
			default:
				return [UIColor blackColor];
		} 
	}
}

- (UIColor*) listViewCellRectColor_Odd {
	if (self.skinsPlist) {
		NSArray* skins = [self.skinsPlist objectForKey:@"skin_lists"];
		NSDictionary* skin_item = [skins objectAtIndex:self.skin_index];
		
		NSString *barColor = [skin_item objectForKey:@"listViewCellRectColorOdd"];
		if (barColor&&![barColor isEmptyOrWhitespace]) {
			return [MCFunction hexColor:barColor];
		} else {
			goto Default_Defined;
		}
	} else {
	Default_Defined:
		switch (self.skin_index) {
			case 1:
				return [MCFunction hexColor:@"E9FFF164"];
			case 2:
				return [MCFunction hexColor:@"A0C8C664"];	
			default:
				return [MCFunction hexColor:@"EAF4FC64"];
		} 
	}
}

- (UIColor*) listViewCellRectColor_Even {
	if (self.skinsPlist) {
		NSArray* skins = [self.skinsPlist objectForKey:@"skin_lists"];
		NSDictionary* skin_item = [skins objectAtIndex:self.skin_index];
		
		NSString *barColor = [skin_item objectForKey:@"listViewCellRectColorEven"];
		if (barColor&&![barColor isEmptyOrWhitespace]) {
			return [MCFunction hexColor:barColor];
		} else {
			goto Default_Defined;
		}
	} else {
	Default_Defined:
		switch (self.skin_index) {
			case 1:
				return [MCFunction hexColor:@"DFFFEB64"];
			case 2:
				return [MCFunction hexColor:@"7BB7B464"];	
			default:
				return [MCFunction hexColor:@"E0EEFA64"];
		} 
	}
}

- (UIColor*) listViewCellLineColor {
	if (self.skinsPlist) {
		NSArray* skins = [self.skinsPlist objectForKey:@"skin_lists"];
		NSDictionary* skin_item = [skins objectAtIndex:self.skin_index];
		
		NSString *barColor = [skin_item objectForKey:@"listViewCellLineColor"];
		if (barColor&&![barColor isEmptyOrWhitespace]) {
			return [MCFunction hexColor:barColor];
		} else {
			goto Default_Defined;
		}
	} else {
	Default_Defined:
		switch (self.skin_index) {
			case 1:
				return [MCFunction hexColor:@"D4FFE564"];
			case 2:
				return [MCFunction hexColor:@"6AAEAB64"];	
			default:
				return [MCFunction hexColor:@"CBE3F764"];
		} 
	}
}

- (UIColor*) listViewCellLineLowColor {
	if (self.skinsPlist) {
		NSArray* skins = [self.skinsPlist objectForKey:@"skin_lists"];
		NSDictionary* skin_item = [skins objectAtIndex:self.skin_index];
		
		NSString *barColor = [skin_item objectForKey:@"listViewCellLineLowColor"];
		if (barColor&&![barColor isEmptyOrWhitespace]) {
			return [MCFunction hexColor:barColor];
		} else {
			goto Default_Defined;
		}
	} else {
	Default_Defined:
		switch (self.skin_index) {
			case 1:
				return [UIColor whiteColor];
			case 2:
				return [MCFunction hexColor:@"ABCFCE64"];	
			default:
				return [UIColor whiteColor];
		} 
	}
}

- (NSDictionary*)skinsPlist
{
	if (nil == skinsPlist) {
		NSFileManager* fileManager = [NSFileManager defaultManager];
		NSString *path = [[MCFunction documentPath] stringByAppendingPathComponent:@"images/skins.plist"];
		
		if ([fileManager fileExistsAtPath:path] && ![self isFirstUse]) {
			return skinsPlist = [[NSDictionary dictionaryWithContentsOfFile:path] retain];
		} else {
			NSString *path = [[NSBundle mainBundle] pathForResource:@"skins" ofType:@"plist"];
			skinsPlist = [[NSDictionary alloc]initWithContentsOfFile:path];
			if(nil != skinsPlist) {
				[skinsPlist retain];
			}
			
			return skinsPlist;
		}
	} else {
		return skinsPlist;
	}
}

- (int) skin_number
{
	if (self.skinsPlist) {
		int skin_number = [[self.skinsPlist objectForKey:@"skin_number"] intValue];
		if (self.skin_index+1>skin_number) {
			self.skin_index = 0;
		}
		
		return MAX(skin_number, packegeSkinNumber);
	} else {
		return packegeSkinNumber <= 3 ? packegeSkinNumber : 0;//固化在开发包里的
	}
}

//index base 0
- (NSString*)skinName:(int)index
{
	if (self.skinsPlist) {
		NSArray* skins = [self.skinsPlist objectForKey:@"skin_lists"];
		NSDictionary* skin_item = [skins objectAtIndex:index];
		
		NSString* name = [skin_item objectForKey:@"skin_name"];
		if (name&&![name isEmptyOrWhitespace]) {
			return name;
		} else {
			goto Default_Defined;
		}
	} else {
	Default_Defined:
		switch (index) {
			case 1:
				return @"浅绿色皮肤"; 
				break;
			case 2:
				return @"深绿色皮肤"; 
				break;
			default:
				return @"蓝色皮肤";
				break;
		}
	}
}

- (BOOL)needLogoHeader {
	return nil != _needLogoHeader && [[_needLogoHeader objectForKey:@"isValid"] boolValue];
}
- (BOOL)hideNavigationBar {
	return nil != _needLogoHeader && [[_needLogoHeader objectForKey:@"hideNavigationBar"] boolValue];
}

- (Class)class:(NSString*)aClass {
	return NSClassFromString(aClass);
}

// 存取
- (int) currentDayReq
{
	NSArray* stringList = [[defaults objectForKey:@"currentDayReq"] componentsSeparatedByString:@":"];
	
	NSCalendar* calendar = [NSCalendar currentCalendar];
	NSUInteger unFlag = NSEraCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit |
	NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit |
	NSSecondCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit;
	NSDateComponents *comps = [calendar components:unFlag fromDate:[NSDate date]];
	if ([stringList count] == 4 &&
		[[stringList objectAtIndex:0] intValue] == [comps year] && 
		[[stringList objectAtIndex:1] intValue] == [comps month] &&
		[[stringList objectAtIndex:2] intValue] == [comps day]) {
		return [[stringList objectAtIndex:3] intValue];
	} else {
		self.currentDayReq = 0;
		return 0;
	}
}
- (void) setCurrentDayReq:(int) currentDayReq
{
	NSCalendar* calendar = [NSCalendar currentCalendar];
	NSUInteger unFlag = NSEraCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit |
	NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit |
	NSSecondCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit;
	NSDateComponents *comps = [calendar components:unFlag fromDate:[NSDate date]];
	
	NSString* data = [NSString stringWithFormat:@"%d:%d:%d:%d", [comps year], [comps month], [comps day], currentDayReq];
	return [defaults setObject:data forKey:@"currentDayReq"];
}

// 存取
- (int) currentDayRes
{
	NSArray* stringList = [[defaults objectForKey:@"currentDayRes"] componentsSeparatedByString:@":"];
	
	NSCalendar* calendar = [NSCalendar currentCalendar];
	NSUInteger unFlag = NSEraCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit |
	NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit |
	NSSecondCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit;
	NSDateComponents *comps = [calendar components:unFlag fromDate:[NSDate date]];
	if ([stringList count] == 4 &&
		[[stringList objectAtIndex:0] intValue] == [comps year] && 
		[[stringList objectAtIndex:1] intValue] == [comps month] &&
		[[stringList objectAtIndex:2] intValue] == [comps day]) {
		return [[stringList objectAtIndex:3] intValue];
	} else {
		self.currentDayRes = 0;
		return 0;
	}
}
- (void) setCurrentDayRes:(int) currentDayRes
{
	NSCalendar* calendar = [NSCalendar currentCalendar];
	NSUInteger unFlag = NSEraCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit |
	NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit |
	NSSecondCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit;
	NSDateComponents *comps = [calendar components:unFlag fromDate:[NSDate date]];
	
	NSString* data = [NSString stringWithFormat:@"%d:%d:%d:%d", [comps year], [comps month], [comps day], currentDayRes];
	return [defaults setObject:data forKey:@"currentDayRes"];
}

// 存取
- (int) currentMonthReq
{
	NSArray* stringList = [[defaults objectForKey:@"currentMonthReq"] componentsSeparatedByString:@":"];
	
	NSCalendar* calendar = [NSCalendar currentCalendar];
	NSUInteger unFlag = NSEraCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit |
	NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit |
	NSSecondCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit;
	NSDateComponents *comps = [calendar components:unFlag fromDate:[NSDate date]];
	if ([stringList count] == 3 &&
		[[stringList objectAtIndex:0] intValue] == [comps year] && 
		[[stringList objectAtIndex:1] intValue] == [comps month]) {
		return [[stringList objectAtIndex:2] intValue];
	} else {
		self.currentMonthReq = 0;
		return 0;
	}
}
- (void) setCurrentMonthReq:(int) currentMonthReq
{
	NSCalendar* calendar = [NSCalendar currentCalendar];
	NSUInteger unFlag = NSEraCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit |
	NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit |
	NSSecondCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit;
	NSDateComponents *comps = [calendar components:unFlag fromDate:[NSDate date]];
	
	NSString* data = [NSString stringWithFormat:@"%d:%d:%d", [comps year], [comps month], currentMonthReq];
	return [defaults setObject:data forKey:@"currentMonthReq"];
}

// 存取
- (int) currentMonthRes
{
	NSArray* stringList = [[defaults objectForKey:@"currentMonthRes"] componentsSeparatedByString:@":"];
	
	NSCalendar* calendar = [NSCalendar currentCalendar];
	NSUInteger unFlag = NSEraCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit |
	NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit |
	NSSecondCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit;
	NSDateComponents *comps = [calendar components:unFlag fromDate:[NSDate date]];
	if ([stringList count] == 3 &&
		[[stringList objectAtIndex:0] intValue] == [comps year] && 
		[[stringList objectAtIndex:1] intValue] == [comps month]) {
		return [[stringList objectAtIndex:2] intValue];
	} else {
		self.currentMonthRes = 0;
		return 0;
	}
}
- (void) setCurrentMonthRes:(int) currentMonthRes
{
	NSCalendar* calendar = [NSCalendar currentCalendar];
	NSUInteger unFlag = NSEraCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit |
	NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit |
	NSSecondCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit;
	NSDateComponents *comps = [calendar components:unFlag fromDate:[NSDate date]];
	
	NSString* data = [NSString stringWithFormat:@"%d:%d:%d", [comps year], [comps month], currentMonthRes];
	return [defaults setObject:data forKey:@"currentMonthRes"];
}

- (NSString*) deviceToken {
	return [defaults objectForKey:@"deviceToken"];
}

- (void) setDeviceToken:(NSString *)deviceToken {
	[defaults setObject:deviceToken forKey:@"deviceToken"];
}

@end
